package com.spring.reject.ImportBeanDefinitionRegistrar.demo1;

import com.spring.reject.Foo;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AssignableTypeFilter;

/**
 *  BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor
 */
public class FooImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
        registerByClassPathBeanDefinitionScanner(registry);
    }

    private void registerByGenericBeanDefinitionClass(BeanDefinitionRegistry registry){
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Foo.class);
        BeanDefinition beanDefinition = builder.getBeanDefinition();
        registry.registerBeanDefinition("foo",beanDefinition);
    }

    //参考https://www.jianshu.com/p/d5ffdccc4f5d  ClassPathBeanDefinitionScanner 原理

    private void  registerByClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry ){
        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
        scanner.addIncludeFilter(new AssignableTypeFilter(Foo.class));
        scanner.scan("com.spring.reject");
    }

}
